package com.magina.antiPro.redisMQ;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
public class MyRedisConf {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                   MessageListenerAdapter listenerAdapter,
                                                   MessageListenerAdapter listenerAdapterTwo,
                                                   MessageListenerAdapter listenerAdapterThree) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("mytopic"));
        //增加订阅者listenerAdapterTwo 对应下面@Bean(参数名称需和监听处理器的方法名称一致，因为@Bean注解默认注入的id就是方法名称)
        container.addMessageListener(listenerAdapterTwo, new PatternTopic("mytopic"));
        //增加频道
        container.addMessageListener(listenerAdapterThree, new PatternTopic("mytopicThree"));
        return container;
    }

    /**
     * 绑定消息监听者和接收监听的方法,必须要注入这个监听器，不然会报错
     *  receiveMessage指定receiver里面的method名称
     */
    @Bean
    public MessageListenerAdapter listenerAdapter() {
        return new MessageListenerAdapter(new Receiver(), "receiveMessage");
    }

    @Bean
    public MessageListenerAdapter listenerAdapterTwo() {
        return new MessageListenerAdapter(new ReceiverTwo(), "receiveMessageTwo");
    }

    @Bean
    public MessageListenerAdapter listenerAdapterThree() {
        return new MessageListenerAdapter(new ReceiverTwo(), "receiveMessageThree");
    }

}

